package org.tsp.android.MapExample;

import java.util.ArrayList;

import org.data.ListMarkersTresor;
import org.data.MarkerTresor;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
        
    private ArrayList<OverlayItem> myOverlays;
	private Activity mycontext;

    public MyItemizedOverlay(Activity context, Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        mycontext = context;
        myOverlays = new ArrayList<OverlayItem>();
        populate();
    }
    
    public Drawable fixMarkerImage(Drawable img)
    {
    	return boundCenterBottom(img);
    }
    
	public void addOverlay(OverlayItem overlay){
	    myOverlays.add(overlay);
	    setLastFocusedIndex(-1);
	    populate();
	}

	public void set(OverlayItem overlay, int index){
		if ( index >= size() )
			myOverlays.add(overlay);
		else	
			myOverlays.set(index, overlay);
		setLastFocusedIndex(-1);
		populate();
	}

    @Override
    protected OverlayItem createItem(int i) {
    	if ( i >= size() )
    		return null;
        return myOverlays.get(i);
    }
        
    // Removes overlay item i
    public void removeItem(int i){
    	if ( i >= size() )
    		return;
    	setLastFocusedIndex(-1);
        myOverlays.remove(i);
        populate();
    }
        
    // Handle tap events on overlay icons
    @Override
    protected boolean onTap(int i){            
    	String message = "";
    	TextView textView = (TextView) mycontext.findViewById(R.id.textPosition);
		
    	if(i < ListMarkersTresor.getInstance().getListMarkerTresors().size())
    	{
	    	MarkerTresor markerTresor = ListMarkersTresor.getInstance().getListMarkerTresors().get(i);	    	
	    	message = "" + markerTresor.getLatitude() + " : " + markerTresor.getLongitude();
    	}
    	
    	textView.setText(message);
        return(true);
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlays.size();
    }
}
